//
//  ClubTopicDetailView.swift
//  77Club
//
//  Created by Apple on 2024/12/26.
//

import UIKit

class ClubSquareSettingItemCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8.0.Scale).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#323233").font(16.font_medium).end()
    private let rightLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(14.font_regular).alignment(.right).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(14.font_regular).line(2).end()
    private let rightImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Square_setting_next").end()
    private let openButton = c_make_ex(UIButton.self)
        .image("Square_setting_off").image("Square_setting_on",state: .selected).end()
    private let bgImageView = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    private let logoImageView = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    
    var updateBanState: ((_ state: String) -> Void)?

    var model: ClubSquareSettingModel?{
        didSet{
            guard let md = model else { return }
            
            titleLabel.text = md.title
            
            if md.title == "论坛禁言"{
                
                rightImageV.isHidden = true
                rightLabel.isHidden = true
                bgImageView.isHidden = true
                logoImageView.isHidden = true
                contentLabel.isHidden = true
//                contentLabel.heightConstraint?.constant = 0
                contentLabel.text = ""
                openButton.isHidden = false
                openButton.isSelected = md.isBan
            }else if md.title == "论坛背景图" {
                
                rightImageV.isHidden = false
                rightLabel.isHidden = true
                openButton.isHidden = true
                logoImageView.isHidden = true
                contentLabel.isHidden = true
//                contentLabel.heightConstraint?.constant = 0
                contentLabel.text = ""
                bgImageView.isHidden = false
                bgImageView.loadImage(url: md.bgImg)
            }else if md.title == "论坛logo" {
                
                rightImageV.isHidden = false
                rightLabel.isHidden = true
                openButton.isHidden = true
                bgImageView.isHidden = true
                contentLabel.isHidden = true
//                contentLabel.heightConstraint?.constant = 0
                contentLabel.text = ""
                logoImageView.isHidden = false
                logoImageView.loadImage(url: md.logo)
            }else if md.title == "论坛介绍" || md.title == "论坛公告"{
                
                rightImageV.isHidden = false
                rightLabel.isHidden = true
                openButton.isHidden = true
                bgImageView.isHidden = true
                logoImageView.isHidden = true
                contentLabel.isHidden = false
                contentLabel.text = md.content
            }else if md.title == "关注设置" {
                
                rightImageV.isHidden = false
                rightLabel.isHidden = false
                openButton.isHidden = true
                logoImageView.isHidden = true
                contentLabel.isHidden = true
                contentLabel.text = ""
                bgImageView.isHidden = true
                rightLabel.text = ""
                rightLabel.attributedText = createAttributedText(from: md.content)
            }else{
                rightImageV.isHidden = false
                rightLabel.isHidden = false
                openButton.isHidden = true
                logoImageView.isHidden = true
                contentLabel.isHidden = true
                contentLabel.text = ""
                bgImageView.isHidden = true
                rightLabel.text = md.content
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                titleLabel
                rightLabel
                rightImageV
                openButton
                contentLabel
                bgImageView
                logoImageView
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(12.0.Scale).bottom(0)
        titleLabel.leading(12.0.Scale).top(18.0.Scale)
        rightImageV.trailing(12.0.Scale).size(20.0.Scale).centerVertically()
        openButton.trailing(12.0.Scale).width(50.0.Scale).height(32.0.Scale).top(16.0.Scale)
        rightLabel.trailing(32.0.Scale).width(200.0.Scale).centerVertically()
        contentLabel.leading(12.0.Scale).trailing(32.0.Scale).top(43.0.Scale).bottom(16.0.Scale)
        bgImageView.trailing(32.0.Scale).width(64.0.Scale).height(36.0.Scale).centerVertically()
        logoImageView.trailing(32.0.Scale).size(36.0.Scale).centerVertically()
        
        openButton.isHidden = true
        rightLabel.isHidden = true
        contentLabel.isHidden = true
        bgImageView.isHidden = true
        logoImageView.isHidden = true
        
        openButton.addTarget(self, action: #selector(clickBanButton), for: .touchUpInside)
    }
    
    @objc private func clickBanButton(){
        openButton.isSelected = !openButton.isSelected
        let state = openButton.isSelected ? "1" : "0"
        updateBanState?(state)
    }
    
    private func createAttributedText(from text: String) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: text)
        // 设置默认颜色
        attributedString.addAttribute(.foregroundColor, value: UIColor.black, range: NSRange(location: 0, length: attributedString.length))
        // 正则匹配所有 #
        let regex = try! NSRegularExpression(pattern: "#", options: [])
        let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: (text as NSString).length))
            
        // 遍历匹配结果，设置颜色
        for (index, match) in matches.enumerated() {
            if index == 0 { // 只改变第一个 # 的颜色
                attributedString.addAttribute(.foregroundColor, value: "#FF7700".toColor, range: match.range)
            }
        }
        return attributedString
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ClubForumMemberSearchView: UIView {
    
    private let searchTField = c_make_ex(UITextField.self)
        .placeholder("请输入用户名称/ID关键词搜索").color("#323233")
        .font(14.font_medium).bgColor("#EDEEF0")
        .corner(16.0.Scale).end()
    
    var searchKeywordBlock:((_:String)->())?
        
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        subviews{
            searchTField
        }
        
        searchTField.leading(16.0.Scale).trailing(16.0.Scale).height(32.0.Scale).top(8.0.Scale)
        
        searchTField.style { v in
            let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 36.0.Scale, height: 32.0.Scale))
            let searchImg = UIImageView(frame: CGRect(x: 12.0.Scale, y: 8.0.Scale, width: 16.0.Scale, height: 16.0.Scale))
            searchImg.image = "Info_search".toImage
            leftView.addSubview(searchImg)
            v.leftView = leftView
            v.leftViewMode = .always
            v.placehold("#AFB1B3".toColor, font: 14.font_medium)
            // 添加监听实时变化
            v.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        }
    }
    
    @objc private func textFieldDidChange(_ textField: UITextField) {
        // 更新带有颜色的文本
        if let text = textField.text {
            searchKeywordBlock?(text)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ClubForumMemberTopView: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(20.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#1A1A1A").font(14.font_semiBold).end()
    private let tagImageV = c_make_ex(UIImageView.self)
        .image("Forum_member_taZhu").end()
    
    var model: ClubForumMemberModel?{
        didSet{
            guard let md = model else { return }
            
            iconImageView.loadImage(url: md.face)
            nameLabel.text = md.pet_name
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            nameLabel
            tagImageV
        }
        
        iconImageView.leading(16.0.Scale).size(40.0.Scale).centerVertically()
        tagImageV.height(18.0.Scale).width(32.0.Scale).trailing(>=16.0.Scale)
        tagImageV.CenterY == iconImageView.CenterY
        nameLabel.leading(68.0.Scale)
        nameLabel.CenterY == iconImageView.CenterY
        nameLabel.Trailing >= tagImageV.Leading - 4
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubForumMemberCell: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(20.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#1A1A1A").font(14.font_semiBold).end()
    private let tagLabel = c_make_ex(UILabel.self)
        .color("#FF9900").font(12.font_semiBold)
        .alignment(.center).corner(4.0.Scale)
        .border("#FF9900", width: 1).text("管理员").end()
    private let muteButton = c_make_ex(UIButton.self)
        .image("Forum_member_aloud").image("Forum_member_mute",state: .selected).end()
    private let managerButton = c_make_ex(UIButton.self)
        .image("Forum_member_manager").image("Forum_member_cancelMan",state: .selected).end()

    var banMemberBlock: ((_ user_id: String, _ type: Int) -> Void)?
    var forumManagerBlock: ((_ user_id: String, _ type: Int) -> Void)?
    
    var isMute = false {
        didSet{
            muteButton.isHidden = !isMute
        }
    }
    var isAdmin = false {
        didSet{
            managerButton.isHidden = isAdmin
        }
    }
    var model: ClubForumMemberModel?{
        didSet{
            guard let md = model else { return }
            
            nameLabel.text = md.pet_name
            iconImageView.loadImage(url: md.face, placehold: "Common_defaultIcon")
            tagLabel.isHidden = !(md.role == 1)
            if ((md.role == 1 || md.role == 2) && isAdmin) || !isMute {
                muteButton.isHidden = true
            }else{
                muteButton.isHidden = false
            }
            managerButton.isSelected = md.role == 1
            muteButton.isSelected = md.is_ban == 1
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            nameLabel
            tagLabel
            muteButton
            managerButton
        }
        
        iconImageView.leading(16.0.Scale).size(40.0.Scale).centerVertically()
        muteButton.trailing(16.0.Scale).size(32.0.Scale).centerVertically()
        managerButton.size(32.0.Scale).centerVertically()
        managerButton.Trailing == muteButton.Leading
        tagLabel.height(18.0.Scale).width(45.0.Scale).centerVertically()
        tagLabel.Leading == nameLabel.Trailing + 4
//        tagLabel.Trailing >= managerButton.Leading - 5
        nameLabel.leading(68.0.Scale).centerVertically()
        
        muteButton.addTarget(self, action: #selector(clickBanButton), for: .touchUpInside)
        managerButton.addTarget(self, action: #selector(clickManagerButton), for: .touchUpInside)
    }
    
    
    @objc private func clickBanButton(){
        
        guard let user_id = model?.user_id else { return }
        muteButton.isSelected = !muteButton.isSelected
        let type = muteButton.isSelected ? 1 : 0
        banMemberBlock?(user_id,type)
    }
    
    
    @objc private func clickManagerButton(){
        
        guard let user_id = model?.user_id else { return }
        managerButton.isSelected = !managerButton.isSelected
        let type = managerButton.isSelected ? 1 : 0
        forumManagerBlock?(user_id,type)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubForumBanMemberCell: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(20.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#1A1A1A").font(14.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(10.font_regular)
        .alignment(.right).end()
    private let cancelButton = c_make_ex(UIButton.self)
        .image("Forum_member_cancelBan").end()

    var isAdmin = false
    var model: ClubForumMemberModel?{
        didSet{
            guard let md = model else { return }
            
            nameLabel.text = md.pet_name
            iconImageView.loadImage(url: md.face, placehold: "Common_defaultIcon")
            timeLabel.text = "禁言时间：" + md.createtime
            if isAdmin && (md.role == 1 || md.role == 2) {
                cancelButton.isHidden = true
            }else{
                cancelButton.isHidden = false
            }
        }
    }
    
    var cancelBanState: ((_ user_id: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            nameLabel
            timeLabel
            cancelButton
        }
        
        iconImageView.leading(16.0.Scale).size(40.0.Scale).centerVertically()
        cancelButton.trailing(16.0.Scale).width(72.0.Scale).height(24.0.Scale).centerVertically()
        timeLabel.trailing(96.0.Scale).centerVertically()
        nameLabel.leading(68.0.Scale).centerVertically()
        nameLabel.Trailing == timeLabel.Leading - 5
        
        cancelButton.addTarget(self, action: #selector(clickCancelBanButton), for: .touchUpInside)
    }
    
    @objc private func clickCancelBanButton(){
        guard let user_id = model?.user_id else { return }
        cancelBanState?(user_id)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubForumManagerMemberCell: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(20.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#1A1A1A").font(14.font_semiBold).end()
    private let cancelButton = c_make_ex(UIButton.self)
        .image("Forum_member_cancelManager").end()

    var model: ClubForumMemberModel?{
        didSet{
            guard let md = model else { return }
            
            nameLabel.text = md.pet_name
            iconImageView.loadImage(url: md.face, placehold: "Common_defaultIcon")
        }
    }
    
    var cancelManager: ((_ user_id: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            nameLabel
            cancelButton
        }
        
        iconImageView.leading(16.0.Scale).size(40.0.Scale).centerVertically()
        cancelButton.trailing(16.0.Scale).width(84.0.Scale).height(24.0.Scale).centerVertically()
        nameLabel.leading(68.0.Scale).centerVertically()
        nameLabel.Trailing == cancelButton.Leading - 5
        
        cancelButton.addTarget(self, action: #selector(clickCancelBanButton), for: .touchUpInside)
    }
    
    
    @objc private func clickCancelBanButton(){
        guard let user_id = model?.user_id else { return }
        cancelManager?(user_id)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
